package com.bonc.modules.sys.service.impl;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.bonc.modules.sys.dao.OriginUserDao;
import com.bonc.modules.sys.dao.SysUserSyncRecordDao;
import com.bonc.modules.sys.entity.OriginUserEntity;
import com.bonc.modules.sys.entity.SysUserEntity;
import com.bonc.modules.sys.entity.SysUserSyncEntity;
import com.bonc.modules.sys.service.OriginUserService;
import com.bonc.modules.sys.service.SysUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;

/**
 * @author jinlong
 * @date 2021/7/1
 */
@Service
public class OriginUserServiceImpl implements OriginUserService {

    @Autowired
    private OriginUserDao originUserDao;

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private SysUserSyncRecordDao sysUserSyncRecordDao;

    @Autowired
    ThreadPoolTaskExecutor threadPoolTaskExecutor;

/*    @Override
    public void syncUser() throws InterruptedException {

        //首先同步闭环中的用户表 tb_pub_user
        Date lastSyncDate = sysUserSyncRecordDao.getLastSync();

        Date currentSyncDate = DateUtil.date();

        int changeUserCount = originUserDao.getChangeUserCount(lastSyncDate);

        CountDownLatch countDownLatch=new CountDownLatch(changeUserCount);
        originUserDao.getOriginUser(lastSyncDate, (resultContext) -> {
            OriginUserEntity originUser = resultContext.getResultObject();
            if (ObjectUtil.isNull(originUser)) {
                return;
            }
            SysUserEntity sysUser = new SysUserEntity();
            sysUser.setUsername(originUser.getUserAccount());
            sysUser.setUserCnName(originUser.getUserName());
            sysUser.setPassword(originUser.getUserPassword());
            sysUser.setMobile(originUser.getUserPhone());
            sysUser.setCityCode(originUser.getUserRegion());
            sysUser.setCityName(originUser.getUserRegionDesc());
            sysUser.setCountyCode(originUser.getUserCity());
            sysUser.setCountyName(originUser.getUserCityDesc());
            sysUser.setRoleIdList(ListUtil.toList(2L));
            // 放在线程池里 使用主数据源
            threadPoolTaskExecutor.submit(() -> {
                try {
                    sysUserService.syncUser(sysUser);
                }finally {
                    countDownLatch.countDown();
                }

            });
        });
        countDownLatch.await();
        SysUserSyncEntity userSyncEntity = new SysUserSyncEntity();
        userSyncEntity.setSyncTime(currentSyncDate);

        sysUserSyncRecordDao.insert(userSyncEntity);

        // 接下来同步经分的用户表 tap_c_user_info
        List<Map<String,String>> portalList = originUserDao.getPortalUserByToday();
        for (Map<String,String> map: portalList) {
            // 遍历当天新增的经分用户是否存在于系统用户表中
            SysUserEntity user = this.sysUserService.queryByUserName(map.get("userId"));
            if(user ==null || StringUtils.isBlank(user.getUsername())){
                user.setUsername(map.get("userId"));
                user.setUserCnName(map.get("userName"));
                user.setPassword("bonc123!@#$%");
                user.setStatus(1);
                user.setCreateUserId(1L);
                user.setMobile(map.get("userPhone"));
                user.setEmail(map.get("userEmail"));
                user.setCityCode(map.get("regionCode"));
                user.setCityName(map.get("regionDesc"));
                user.setCountyCode(map.get("areaCode"));
                user.setCountyName(map.get("areaDesc"));
                user.setRoleIdList(ListUtil.toList(2L));
                sysUserService.saveUser(user);
            }
        }
    }*/

    @Override
    public void syncUser() throws InterruptedException {
        Date lastSyncDate = sysUserSyncRecordDao.getLastSync();

        Date currentSyncDate = DateUtil.date();

        int changeUserCount = originUserDao.getChangeUserCount(lastSyncDate);

        CountDownLatch countDownLatch=new CountDownLatch(changeUserCount);
        originUserDao.getOriginUser(lastSyncDate, (resultContext) -> {
            OriginUserEntity originUser = resultContext.getResultObject();
            if (ObjectUtil.isNull(originUser)) {
                return;
            }
            SysUserEntity sysUser = new SysUserEntity();
            sysUser.setUsername(originUser.getUserAccount());
            sysUser.setUserCnName(originUser.getUserName());
            sysUser.setPassword(originUser.getUserPassword());
            sysUser.setMobile(originUser.getUserPhone());
            sysUser.setCityCode(originUser.getUserRegion());
            sysUser.setCityName(originUser.getUserRegionDesc());
            sysUser.setCountyCode(originUser.getUserCity());
            sysUser.setCountyName(originUser.getUserCityDesc());
            sysUser.setRoleIdList(ListUtil.toList(2L));
            // 放在线程池里 使用主数据源
            threadPoolTaskExecutor.submit(() -> {
                try {
                    sysUserService.syncUser(sysUser);
                }finally {
                    countDownLatch.countDown();
                }

            });
        });
        countDownLatch.await();
        SysUserSyncEntity userSyncEntity = new SysUserSyncEntity();
        userSyncEntity.setSyncTime(currentSyncDate);

        sysUserSyncRecordDao.insert(userSyncEntity);
    }
}
